#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
污染类型调试API - 简化版本，不带权限检查
"""

from fastapi import APIRouter, Query
from typing import List, Optional, Dict, Any
from services.pollution_type_service import pollution_type_service
from core.logging_config import get_logger
import time

logger = get_logger("pollution_types_debug")

router = APIRouter(prefix="/pollution-types-debug", tags=["pollution-types-debug"])

@router.get("/", response_model=dict)
async def get_pollution_types_debug(
    keyword: Optional[str] = Query(None, description="搜索关键词"),
    active_only: bool = Query(True, description="仅显示激活的类型"),
    tree_view: bool = Query(False, description="返回树形结构"),
    customer_id: str = Query("default", description="客户标识"),
):
    """获取污染类型列表 - 调试版本，不带权限检查"""
    start_time = time.time()

    try:
        logger.info(f"DEBUG: 开始处理调试请求 - 客户: {customer_id}")

        if tree_view:
            data = pollution_type_service.get_hierarchy_tree(customer_id)
        elif keyword:
            data = pollution_type_service.search_pollution_types(customer_id, keyword)
        else:
            data = pollution_type_service.get_all_pollution_types(customer_id, active_only)

        logger.info(f"DEBUG: 调试请求成功处理 - 数据数量: {len(data) if isinstance(data, list) else 'not a list'}")

        return {
            "code": 200,
            "message": "获取成功（调试版本）",
            "data": data,
            "count": len(data) if isinstance(data, list) else 0,
            "timestamp": time.time()
        }
    except Exception as e:
        logger.error(f"DEBUG: 调试API异常: {str(e)}")
        import traceback
        logger.error(f"DEBUG: 完整堆栈跟踪: {traceback.format_exc()}")
        return {
            "code": 500,
            "message": f"调试API异常: {str(e)}",
            "timestamp": time.time()
        }